package com.ssy.lingxi.platform.template.service.serviceimpl.mobile;

import cn.hutool.core.collection.CollectionUtil;
import com.ssy.lingxi.common.constant.template.PageTemplateTypeEnum;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.platform.template.service.constant.CommonBooleanEnum;
import com.ssy.lingxi.platform.template.service.entity.adorn.AppAdorn;
import com.ssy.lingxi.platform.template.service.entity.adorn.PageTemplate;
import com.ssy.lingxi.platform.template.service.entity.door.MemberSelf;
import com.ssy.lingxi.platform.template.service.model.dto.mobile.MemberSelfMainMobileDTO;
import com.ssy.lingxi.platform.template.service.model.vo.mobile.MemberSelfMainMobileVO;
import com.ssy.lingxi.platform.template.service.repository.AppAdornRepository;
import com.ssy.lingxi.platform.template.service.repository.MemberSelfRepository;
import com.ssy.lingxi.platform.template.service.repository.PageTemplateRepository;
import com.ssy.lingxi.platform.template.service.service.mobile.MemberSelfMobileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @menu mobile - 会员自营 - 业务实现层
 * @author fhj
 * @version 2.0.0
 * @date 2021/10/19
 */
@Slf4j
@Service
public class MemberSelfMobileServiceImpl implements MemberSelfMobileService {

    @Resource
    private MemberSelfRepository memberSelfRepository;
    @Resource
    private PageTemplateRepository pageTemplateRepository;
    @Resource
    private AppAdornRepository appAdornRepository;

    @Override
    public Wrapper<MemberSelfMainMobileVO> memberSelfMain(MemberSelfMainMobileDTO dto) {
        //根据会员ID获取会员自营门户
        MemberSelf memberSelf = memberSelfRepository.findByMemberId(dto.getMemberId());
        if(memberSelf == null) {
            return Wrapper.fail(ResponseCode.PT_RECORDS_DON_T_EXIST);
        }

        //拷贝
        MemberSelfMainMobileVO memberSelfMainMobileVO = new MemberSelfMainMobileVO();
        BeanUtils.copyProperties(memberSelf, memberSelfMainMobileVO);

        //封装正在使用的自营商城模板装修内容
        List<PageTemplate> pageTemplateList = pageTemplateRepository.findByTypeAndStatusAndShopId(
                PageTemplateTypeEnum.SELF.getCode(),
                CommonBooleanEnum.YES.getCode(),
                dto.getShopId()
        );
        if(CollectionUtil.isNotEmpty(pageTemplateList)) {
            pageTemplateList = pageTemplateList.stream().filter(a ->
                    CollectionUtil.isNotEmpty(a.getUseSelfTemplate()) &&
                            a.getUseSelfTemplate().stream().anyMatch(b ->
                                    b.getMemberId().equals(memberSelf.getMemberId()) && b.getRoleId().equals(memberSelf.getRoleId())
                            )
            ).collect(Collectors.toList());

            if(CollectionUtil.isNotEmpty(pageTemplateList)) {
                if (pageTemplateList.size() > 1) {
                    log.error("正在使用的自营商城模板出现多个【检查原因】");
                }
                PageTemplate pageTemplate = pageTemplateList.get(0);

                AppAdorn appAdorn = appAdornRepository.findByTemplateIdAndMemberIdAndRoleId(
                        pageTemplate.getId(),
                        memberSelf.getMemberId(),
                        memberSelf.getRoleId()
                );
                memberSelfMainMobileVO.setTemplateId(pageTemplate.getId());
                if(appAdorn != null) {
                    memberSelfMainMobileVO.setAdornContent(appAdorn.getAdornContent());
                }
            }
        }

        return Wrapper.success(memberSelfMainMobileVO);
    }
}
